package com.gitee.wsl.struct.grammar.ast.visitor

import com.gitee.wsl.struct.grammar.ast.node.ASTNode
import com.gitee.wsl.struct.grammar.ast.node.CompositeNode
import com.gitee.wsl.struct.grammar.ast.node.LeafNode


abstract class AbstractVisitor: Visitor {
    override fun visitNode(node: ASTNode) {
        /*if (node is CompositeASTNode) {
            dispatchCompositeToVisitStart(node)
            for (child in node.children) {
                visitNode(child)
            }
            dispatchCompositeToVisitEnd(node)
        } else if(node is LeafASTNode)
                visitLeaf(node)
            else
                dispatchToVisit(node)*/

        when(node){
            is LeafNode -> visitLeaf(node)

            is CompositeNode ->{
                dispatchCompositeToVisitStart(node)
                for (child in node.children) {
                    visitNode(child)
                }
                dispatchCompositeToVisitEnd(node)
            }

            else -> dispatchToVisit(node)
        }

    }

    abstract fun dispatchCompositeToVisitStart(ast: CompositeNode)

    abstract fun dispatchCompositeToVisitEnd(ast: CompositeNode)

    abstract fun dispatchToVisit(ast: ASTNode)

}